package com.party.notify.notifyPush.servce.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.common.constant.Constant;
import com.party.common.redis.StringJedis;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.MessageTypeModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.coupon.Coupon;
import com.party.core.model.crowdfund.*;
import com.party.core.model.gatherForm.GatherProject;
import com.party.core.model.gift.Gift;
import com.party.core.model.goods.Goods;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberAct;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.ThirdPartyUser;
import com.party.core.model.message.Message;
import com.party.core.model.message.MessageTag;
import com.party.core.model.notify.InstanceConstant;
import com.party.core.model.notify.InstanceWay;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.order.OrderForm;
import com.party.core.model.order.OrderStatus;
import com.party.core.model.order.OrderType;
import com.party.core.model.sign.SignProject;
import com.party.core.model.sign.SignRecord;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.coupon.ICouponService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.goods.IGoodsService;
import com.party.core.service.member.*;
import com.party.core.service.sign.ISignProjectService;
import com.party.core.service.sign.ISignRecordService;
import com.party.core.service.system.ISysConfigService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.notify.notifyPush.servce.INotifyService;
import com.party.notify.wechatNotify.service.IWechatKeynotService;
import com.party.notify.wechatNotify.service.IWechatNotifyService;
import com.vdurmont.emoji.EmojiParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 消息推送统一接口实现
 * Created by wei.li
 *
 * @date 2017/4/18 0018
 * @time 16:56
 */

@Service
public class NotifySendService implements INotifySendService {

    @Autowired
    private StringJedis stringJedis;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private INotifyService notifyService;

    @Autowired
    private IWechatKeynotService wechatKeynotService;
    
    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private ISupportService supportService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private IPackageCountService packageCountService;

    @Autowired
    private ISignProjectService signProjectService;

    @Autowired
    private ISignRecordService signRecordService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IWechatNotifyService wechatNotifyService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IThirdPartyUserService thirdPartyUserService;

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private IMemberGroupService memberGroupService;

    // 验证码过期时间
    private final static long TIME_OUT = 3600;

    private final static String GOOD_BUY_CODE ="goodBuy";

    private final static String ACTIVITY_APPLY_CODE = "apply";

    private final static String COURSE_APPLY_CODE = "courseApply";

    private final static String CROWFUND = "crowfund";

    private final static String FREE_APPLY_REFUSE = "freeApplyRefuse";

    private final static String PAY_APPLY_REFUSE = "payApplyRefuse";

    private final static String APPLY_CHEACK_PASS = "applyCheackPass";

    private final static String GOODS_REFUND = "goodsRefund";

    private final static String ACTIVITY_REFUND = "activityRefund";

    private final static String EXPERT_APPROVE = "expertApprove";

    private final static String EXPERT_CANCEL = "expertCancel";

    private final static String LOVE = "love";

    private final static String COMMENT = "comment";

    private final static String FOCUS = "focus";

    private final static String MEMBER_CHEACK_PASS = "memberCheackPass";

    private final static String MEMBER_CHEACK_REFUSE = "memberCheackRefuse";

    private final static String ACTIVITY_CHEACK_PASS = "activityCheackPass";

    private final static String ACTIVITY_CHEACK_REFUSE = "activityCheackRefuse";

    private final static String SUPPORT_STAGE = "supportStage";

    private final static String PROJECT_STAGE = "projectStage";

    private final static String LAUNCH_PROJECT = "launchProject";

    private final static String MOBILE_CODE = "mobileCode";

    private final static String ACCEPT_SUPPORT = "acceptSupport";

    private final static String ALL_CROWDFUND = "allCrowdfundPush";


    // 多场活动的众筹主动推送
    //众筹成功
    private final static String MULTIPLE_CROWFUND_SUCCESS = "multipleCrowfundSuccess";
    //众筹发起
    private final static String MULTIPLE_LAUNCH_PROJECT = "multipleLaunchProject";

    private final static String MULTIPLE_PUSH = "multiplePush";

    private final static String UNDERWAY_CORDFUND = "underwayCordfundPush";

    private final static String SUCCESS_CROWDFUND = "successCrowdfundPush";
    
    private final static String SUCCESS_FORM = "successFormPush";

    private final static String SUCCESS_BUY_LEVEL = "successBuyLevel";

    private static final String DATA_WARN = "dataWarn";

    private static final String CROWFUND_COMMENT = "crowfundComment";

    protected static Logger logger = LoggerFactory.getLogger(NotifySendService.class);

    private static final String ADMIN_ID = "32f133dd6f764d7681437cc4521daa17";

    // 票券领取成功推送
    private static final String SUCCESS_COUPON_ORDER = "successCouponOrder";
    // 实物礼包领取成功推送
    private static final String SUCCESS_GIFT_ORDER = "successGiftOrder";

    // 通知打卡
    private static final String NOTIFY_SIGN = "notifySign";
    // 打卡成功
    private static final String SUCCESS_SIGN = "successSign";
    // 打卡排名
    private static final String NOTIFY_SIGN_RANK = "notifySignRank";

    @Value("${mobile.domain}")
    private String domain;

    /**
     * 发送验证码
     * @param phone 手机号
     */
    public void sendVerifyCode(String phone) {
        // 六位随机数
        DecimalFormat format = new DecimalFormat("000000");
        String code = format.format(Math.random() * 999999);

        // 缓存验证码
        stringJedis.setValue(phone, code, TIME_OUT);
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{code}", code);
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(MOBILE_CODE, phone, null, constant);
    }

    /**
     * 下单成功发送
     * @param orderForm 订单
     * @param verifyCode 验证码
     */
    public void senGoodsBuy(OrderForm orderForm, String verifyCode) {
        Goods goods = goodsService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", goods.getTitle());
        constant.put("{verCode}", verifyCode);
        constant.put("{address}", goods.getPlace());
        constant.put("logo", Message.MSG_LOGO_GOODS);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", goods.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(goods.getId()));
        constant.put("senderId", Strings.nullToEmpty(goods.getMemberId()));

        Member author = memberService.get(orderForm.getMemberId());
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(author.getRealname());//下单者
        JSONObject keyword2 = wechatKeynotService.get(orderForm.getId());//订单号
        JSONObject keyword3 = wechatKeynotService.get(orderForm.getPayment().toString());//金额
        JSONObject keyword4 = wechatKeynotService.get(orderForm.getTitle());//商品信息
        jsonData.put("keyword1", keyword1);
        jsonData.put("keyword2", keyword2);
        jsonData.put("keyword3", keyword3);
        jsonData.put("keyword4", keyword4);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", orderForm.getId());
        constant = wechatKeynotService.getWechatConstant(GOOD_BUY_CODE, orderForm.getMemberId(), constant, jsonData, urlConstant);
        notifyService.push(GOOD_BUY_CODE, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 报名成功推送
     *
     * @param orderForm  订单
     * @param verifyCode 验证码
     * @param activity   活动
     * @param orgMember  发布者
     */
    @Override
    public void sendActivityApply(OrderForm orderForm, String verifyCode, Activity activity, MemberGroup orgMember) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("{verCode}", verifyCode);
        constant.put("{address}", activity.getPlace());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());

        String partnerName = getPartnerName(orgMember);
        constant.put("{partner}", Strings.nullToEmpty(partnerName));

        //微信推送
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getTitle());//标题
        JSONObject keyword2 = wechatKeynotService.get(orgMember.getRealname());//主办方
        JSONObject keyword3 = wechatKeynotService.get(DateUtils.formatDate(activity.getStartTime()));//时间
        jsonData.put("keyword1", keyword1);
        jsonData.put("keyword2", keyword2);
        jsonData.put("keyword3", keyword3);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", orderForm.getGoodsId());
        constant = wechatKeynotService.getWechatConstant(ACTIVITY_APPLY_CODE, orderForm.getMemberId(), constant, jsonData, urlConstant);
        notifyService.push(ACTIVITY_APPLY_CODE, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 获取机构名称
     *
     * @param orgMember 原机构
     * @return 新的机构名称
     * @author yifeng
     * @date 15:55 2019/9/17
     **/
    private String getPartnerName(MemberGroup orgMember) {
        // 短信签名
        MemberGroup partner = memberGroupService.getPartner(orgMember.getId());
        return partner.getRealname();
    }

    /**
     * 报名成功发送
     * @param orderForm 订单
     * @param verifyCode 验证码
     * @param activity 课程活动
     */
    @Override
    public void sendCourseApply(OrderForm orderForm, String verifyCode, Activity activity) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("{verCode}", verifyCode);
        constant.put("{address}", activity.getPlace());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(COURSE_APPLY_CODE, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 商品退款推送
     * @param orderForm 订单
     */
    public void sendGoodsRefund(OrderForm orderForm) {
        Goods goods = goodsService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", goods.getTitle());
        constant.put("logo", Message.MSG_LOGO_GOODS);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", goods.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(goods.getId()));
        constant.put("senderId", Strings.nullToEmpty(goods.getMemberId()));

        //微信消息
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getPayment().toString());//退款金额
        JSONObject keyword2 = wechatKeynotService.get(orderForm.getTitle());//商品详情
        JSONObject keyword3 = wechatKeynotService.get(orderForm.getId());//订单编号
        jsonData.put("orderProductPrice",keyword1);
        jsonData.put("orderProductName",keyword2);
        jsonData.put("orderName",keyword3);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", orderForm.getGoodsId());
        constant = wechatKeynotService.getWechatConstant(GOODS_REFUND, orderForm.getMemberId(), constant, jsonData, urlConstant);
        notifyService.push(GOODS_REFUND, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 活动退款推送
     * @param orderForm 订单
     */
    public void sendActivityRefund(OrderForm orderForm) {
        Activity activity = activityService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("logo", Message.MSG_LOGO_GOODS);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));
        getActivityPrtner(activity, constant);

        //微信消息
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getPayment().toString());//退款金额
        JSONObject keyword2 = wechatKeynotService.get(orderForm.getTitle());//商品详情
        JSONObject keyword3 = wechatKeynotService.get(orderForm.getId());//订单编号
        jsonData.put("orderProductPrice",keyword1);
        jsonData.put("orderProductName",keyword2);
        jsonData.put("orderName",keyword3);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", orderForm.getGoodsId());
        constant = wechatKeynotService.getWechatConstant(ACTIVITY_REFUND, orderForm.getMemberId(), constant, jsonData, urlConstant);
        notifyService.push(ACTIVITY_REFUND, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 众筹成功发送
     *
     * @param orderForm       订单
     * @param verifyCode      验证码
     * @param crowdfundMember 众筹者
     * @param orgMember       发布者
     */
    @Override
    public void sendCorowdfund(OrderForm orderForm, String verifyCode, Member crowdfundMember, MemberGroup orgMember) {
        Activity activity = activityService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("{verCode}", verifyCode);
        constant.put("{address}", activity.getPlace());
        constant.put("{user}", crowdfundMember.getRealname());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));

        String partnerName = getPartnerName(orgMember);
        constant.put("{partner}", Strings.nullToEmpty(partnerName));
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());

        //微信推送
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getTitle());//标题
        JSONObject keyword2 = wechatKeynotService.get(orderForm.getPayment().toString());//标题
        JSONObject keyword3 = wechatKeynotService.get(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm"));//时间

        JSONObject first = wechatKeynotService.get("恭喜贺喜，您在[" + orgMember.getRealname() + "]的众筹活动已经成功");
        JSONObject remark = wechatKeynotService.get("[" + orgMember.getRealname() + "]温馨提醒");

        jsonData.put("first", first);
        jsonData.put("keyword1", keyword1);
        jsonData.put("keyword2", keyword2);
        jsonData.put("keyword3", keyword3);
        jsonData.put("remark", remark);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        TargetProject targetProject = targetProjectService.findByOrderId(orderForm.getId());
        urlConstant.put("{id}", targetProject.getProjectId());
        constant = wechatKeynotService.getWechatConstant(activity.getMember(), CROWFUND, crowdfundMember.getId(), constant, jsonData, urlConstant);

        CrowdfundEvent crowdfundEvent = crowdfundEventService.findByProjectId(targetProject.getProjectId());
        if (null != crowdfundEvent && YesNoStatus.YES.getCode().equals(crowdfundEvent.getIsMultiple())) {
            constant.put("templateUrl", domain + "/micWeb/html/project/project_multi_detail.html?id=" + targetProject.getProjectId());
        }

        notifyService.push(CROWFUND, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 免费活动拒绝审核发送
     * @param orderForm 订单
     */
    public void sendFreeCheack(OrderForm orderForm) {
        Activity activity = activityService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());

        if (OrderType.ORDER_ACTIVITY.getCode().equals(orderForm.getType())) {
            constant.put("senderId", activity.getMember());
            getActivityPrtner(activity, constant);
            constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());
        } else if (OrderType.ORDER_COURSE.getCode().equals(orderForm.getType())) {
            constant.put("senderId", ADMIN_ID);
        }
        notifyService.push(FREE_APPLY_REFUSE, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 收费活动拒绝审核发送
     * @param orderForm 订单
     */
    public void sendPayCheack(OrderForm orderForm) {
        Activity activity = activityService.get(orderForm.getGoodsId());
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        if (OrderType.ORDER_ACTIVITY.getCode().equals(orderForm.getType())) {
            constant.put("senderId", activity.getMember());
            getActivityPrtner(activity, constant);
            constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());
        } else if (OrderType.ORDER_COURSE.getCode().equals(orderForm.getType())) {
            constant.put("senderId", ADMIN_ID);
        }
        notifyService.push(PAY_APPLY_REFUSE, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }

    /**
     * 审核通过发送
     * @param activity 活动
     * @param orderId 订单号
     * @param phone 电话
     * @param memberId 会员编号
     * @param orderStatus 订单状态
     */
    public void sendApplyPass(Activity activity, String orderId, String phone, String memberId, Integer orderStatus) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        getActivityPrtner(activity, constant);
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderId);
        constant.put("orderStatus", orderStatus);
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", memberId);
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(APPLY_CHEACK_PASS, phone, memberId, constant);
    }

    /**
     * 达人认证推送
     */
    public void sendExpertApprove(String memberId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("logo", Message.MSG_LOGO_MEMBER);
        constant.put("tag", MessageTypeModel.MESSAGE_TAG_MEMBER);
        constant.put("createBy", memberId);
        notifyService.push(EXPERT_APPROVE, memberId, constant);
    }

    /**
     * 达人取消推送
     */
    public void sendExpertCancel(String memberId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("logo", Message.MSG_LOGO_MEMBER);
        constant.put("tag", MessageTypeModel.MESSAGE_TAG_MEMBER);
        constant.put("createBy", memberId);
        notifyService.push(EXPERT_CANCEL, memberId, constant);
    }

    /**
     * 点赞推送
     * @param memberId 会员
     * @param author 作者
     * @param logo 标志
     * @param url 路径
     * @param content 内容
     * @param relId 关联编号
     */
    public void sendLove(String memberId, Member author, String logo, String url, String content, String relId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", author.getRealname());
        constant.put("logo", logo);
        constant.put("url", url);
        constant.put("constant", content);
        constant.put("relId", relId);
        constant.put("createBy", author.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        notifyService.push(LOVE, memberId, constant);
    }

    /**
     * 关注推送
     * @param memberId 会员
     * @param author 作者
     * @param logo 标志
     * @param url 路径
     * @param content 内容
     * @param relId 关联编号
     */
    public void sendFocus(String memberId, Member author, String logo, String url, String content, String relId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", author.getRealname());
        constant.put("logo", logo);
        constant.put("url", url);
        constant.put("constant", content);
        constant.put("relId", relId);
        constant.put("createBy", author.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        notifyService.push(FOCUS, memberId, constant);
    }

    /**
     * 评论推送
     * @param memberId 会员
     * @param author 作者
     * @param logo 标志
     * @param url 路径
     * @param content 内容
     * @param relId 关联编号
     */
    public void sendComment(String memberId, Member author, String logo, String url, String content, String relId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", author.getRealname());
        constant.put("logo", logo);
        constant.put("url", url);
        constant.put("constant", content);
        constant.put("relId", relId);
        constant.put("createBy", author.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        notifyService.push(COMMENT, memberId, constant);
    }

    /**
     * 会员审核通过
     * @param member 会员
     */
    public void sendMemberPass(Member member) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("createBy", member.getId());
        constant.put("logo", MessageTypeModel.MSG_LOGO_MEMBER);
        constant.put("tag", MessageTypeModel.MESSAGE_TAG_MEMBER);
        notifyService.push(MEMBER_CHEACK_PASS, member.getMobile(), member.getId(), constant);
    }

    /**
     * 会员审核拒绝
     * @param member 会员
     */
    public void sendMemberRefuse(Member member) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("createBy", member.getId());
        constant.put("logo", MessageTypeModel.MSG_LOGO_MEMBER);
        constant.put("tag", MessageTypeModel.MESSAGE_TAG_MEMBER);
        notifyService.push(MEMBER_CHEACK_REFUSE, member.getMobile(), member.getId(), constant);
    }


    /**
     * 活动审核通过
     * @param title 题目
     * @param relId 关联编号
     * @param memberId 会员编号
     */
    public void sendActivityPass(String title, String relId, String memberId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", title);
        constant.put("logo", MessageTypeModel.MSG_LOGO_ACT);
        constant.put("relId", relId);
        constant.put("createBy", memberId);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(ACTIVITY_CHEACK_PASS, memberId, constant);
    }

    /**
     * 活动审核拒绝
     * @param title 题目
     * @param relId 关联编号
     * @param memberId 会员编号
     */
    public void sendActivityRefuse(String title, String relId, String memberId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", title);
        constant.put("logo", MessageTypeModel.MSG_LOGO_ACT);
        constant.put("relId", relId);
        constant.put("createBy", memberId);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(ACTIVITY_CHEACK_REFUSE, memberId, constant);
    }


    /**
     * 发送支持阶段提醒的推送
     * @param orderForm 订单编号
     * @param project 众筹项目
     * @param member 会员编号
     */
    public void sendSupportStage(OrderForm orderForm, Project project,Member member){
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{favorerNum}", project.getFavorerNum());
        constant.put("{payment}", orderForm.getPayment());
        constant.put("{actualAmount}", project.getActualAmount());
        getProjectSender(project.getId(), constant);
        getProjectPartner(project.getId(), constant);
        notifyService.push(SUPPORT_STAGE, member.getMobile(), member.getId(), constant);
    }


    /**
     * @param project         众筹
     * @param crowdfundMember 众筹者
     * @param percent         百分比
     * @param rankNo          排名
     * @param orgMember       发布者
     * @param activity        项目
     * @return
     * @Author yifeng
     * @Description 发送项目阶段提醒
     * @Date 15:42 2019/4/11
     **/
    @Override
    public void sendProjectStage(Project project, Member crowdfundMember, int percent, int rankNo, MemberGroup orgMember, Activity activity) {
        HashMap<String, Object> constant = Maps.newHashMap();
        Float balance = BigDecimalUtils.sub(project.getTargetAmount(), project.getActualAmount());
        balance = BigDecimalUtils.round(balance, 2);
        constant.put("{user}", crowdfundMember.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{percent}", percent);
        constant.put("{rankNo}", rankNo);
        constant.put("{actualAmount}", project.getActualAmount());
        constant.put("{favorerNum}", project.getFavorerNum());
        constant.put("{balance}", balance);
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());

        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));

        String partnerName = getPartnerName(orgMember);
        constant.put("{partner}", Strings.nullToEmpty(partnerName));

        notifyService.push(PROJECT_STAGE, crowdfundMember.getMobile(), crowdfundMember.getId(), constant);
    }

    /**
     * 发起众筹消息推送
     * @param project 众筹
     * @param member 会员编号
     */
    public void sendLaunchProject(Project project, Member member) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{targetAmount}", project.getTargetAmount());
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());
        getProjectSender(project.getId(), constant);
        getProjectPartner(project.getId(), constant);
        notifyService.push(LAUNCH_PROJECT, member.getMobile(), member.getId(), constant);
    }

    /**
     * 获取众筹发送者
     * @param projectId 众筹编号
     * @param constant 内容
     */
    public void getProjectSender(String projectId, HashMap<String, Object> constant){
        TargetProject targetProject = targetProjectService.findByProjectId(projectId);
        Activity activity = activityService.get(targetProject.getTargetId());
        if (null != activity){
            constant.put("targetId", Strings.nullToEmpty(activity.getId()));
            constant.put("senderId", Strings.nullToEmpty(activity.getMember()));
        }
    }

    /**
     * 获取合作商名称
     * @param projectId 众筹编号
     * @param constant 内容
     */
    @Override
    public void getProjectPartner(String projectId, HashMap<String, Object> constant){
        TargetProject targetProject = targetProjectService.findByProjectId(projectId);
        Activity activity = activityService.get(targetProject.getTargetId());
        if (null == activity){
            throw new BusinessException("众筹活动不存在");
        }
        MemberGroup member = memberGroupService.getPartner(activity.getMember());
        if (null == member){
            throw new BusinessException("众筹活动发起者不存在");
        }
        constant.put("{partner}", Strings.nullToEmpty(member.getRealname()));
    }

    /**
     * 众筹收到支持消息推送
     *
     * @param orderForm       订单信息
     * @param supportMember   支持者
     * @param crowdfundMember 接受者
     * @param orgMember       发布者
     * @param projectId       众筹id
     */
    @Override
    public void sendAcceptSupport(OrderForm orderForm, Member supportMember, Member crowdfundMember, String projectId, MemberGroup orgMember) {
        HashMap<String, Object> constant = Maps.newHashMap();
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getTitle());//标题
        JSONObject keynote2 = wechatKeynotService.get(supportMember.getRealname());//支持者
        JSONObject keynote3 = wechatKeynotService.get(orderForm.getPayment().toString());//金额
        JSONObject keynote4 = wechatKeynotService.get(DateUtils.formatDate(orderForm.getCreateDate(), "yyyy-MM-dd HH:mm"));//时间
        jsonData.put("keyword1", keyword1);
        jsonData.put("keyword2", keynote2);
        jsonData.put("keyword3", keynote3);
        jsonData.put("keyword4", keynote4);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", projectId);
        constant = wechatKeynotService.getWechatConstant(orgMember.getId(), ACCEPT_SUPPORT, crowdfundMember.getId(), constant, jsonData, urlConstant);

        CrowdfundEvent crowdfundEvent = crowdfundEventService.findByProjectId(projectId);
        if (null != crowdfundEvent && YesNoStatus.YES.getCode().equals(crowdfundEvent.getIsMultiple())) {
            constant.put("templateUrl", domain + "/micWeb/html/project/project_multi_detail.html?id=" + projectId);
        }

        //特殊处理众筹支持消息
        Support support = supportService.findByOrderId(orderForm.getId());
        if (StringUtils.isNotEmpty(support.getComment())) {
            if (sysConfigService.isConvertBase64()) {
                support.setComment(StringUtils.decodeBase64New(support.getComment()));
            } else {
                support.setComment(EmojiParser.parseToUnicode(support.getComment()));
            }
        }

        JSONObject remark = wechatKeynotService.get(support.getComment() + "\n[" + orgMember.getRealname() + "]温馨提醒");
        jsonData.put("remark", remark);

        notifyService.push(ACCEPT_SUPPORT, crowdfundMember.getMobile(), crowdfundMember.getId(), constant);
    }

    /**
     * 收到众筹评论推送
     * @param commentName 评论人名字
     * @param comment 评论内容
     * @param activityMemberId 众筹项目发起者
     * @param projectId 众筹id
     * @param authorId 发送给谁
     */
    public void sendAcceptCrowfundComment(String commentName, String comment, String authorId, String activityMemberId, String projectId) {
        if (StringUtils.isEmpty(commentName)) {
            commentName = "无名";
        }
        HashMap<String, Object> constant = Maps.newHashMap();
        JSONObject jsonData = new JSONObject();
        // 评论内容
        JSONObject keyword1 = wechatKeynotService.get(comment);
        // 评论时间
        JSONObject keynote2 = wechatKeynotService.get(DateUtils.formatDate(new Date(), "yyyy年MM月dd日 HH:mm"));
        jsonData.put("keyword1", keyword1);
        jsonData.put("keyword2", keynote2);

        MemberGroup orgMember = memberGroupService.get(activityMemberId);
        JSONObject remark = wechatKeynotService.get("[" + orgMember.getRealname() + "]温馨提醒");
        jsonData.put("remark", remark);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", projectId);
        constant = wechatKeynotService.getWechatConstant(activityMemberId, CROWFUND_COMMENT, authorId, constant, jsonData, urlConstant);

        CrowdfundEvent crowdfundEvent = crowdfundEventService.findByProjectId(projectId);
        if (null != crowdfundEvent && YesNoStatus.YES.getCode().equals(crowdfundEvent.getIsMultiple())) {
            constant.put("templateUrl", domain + "/micWeb/html/project/project_multi_detail.html?id=" + projectId);
        }

        // 处理first有占位符的情况
        JSONObject jsonFirst = (JSONObject) jsonData.get("first");
        String first = jsonFirst.get("value").toString();
        first = StringUtils.replace(first, "{name}", commentName);
        jsonFirst.remove("value");
        jsonFirst.put("value", first);
        jsonData.remove("first");
        jsonData.put("first", jsonFirst);

        notifyService.push(CROWFUND_COMMENT, authorId, constant);
    }

    /**
     * 免费活动报名
     * @param memberActId 活动报名ID
     */
	public void sendActivityApplyFree(String activityId, String memberActId, String verifyCode) {
		Activity activity = activityService.get(activityId);
		MemberAct memberAct = memberActService.get(memberActId);
		HashMap<String, Object> constant = Maps.newHashMap();
		constant.put("{title}", activity.getTitle());
		constant.put("{verCode}", verifyCode);
		constant.put("{address}", activity.getPlace());
        getActivityPrtner(activity, constant);
		constant.put("logo", Message.MSG_LOGO_ACT);
		constant.put("relId", activity.getId());
		constant.put("tag", MessageTag.UNDEFIND.getCode());
		constant.put("orderId", memberActId);
		constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
		constant.put("createBy", memberAct.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activityId));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());
		notifyService.push(ACTIVITY_APPLY_CODE, memberAct.getMobile(), memberAct.getMemberId(), constant);
	}

    /**
     * 免费课程报名
     * @param memberActId 活动报名ID
     */
    @Override
    public void sendCourseApplyFree(String activityId, String memberActId, String verifyCode) {
        Activity activity = activityService.get(activityId);
        MemberAct memberAct = memberActService.get(memberActId);
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", activity.getTitle());
        constant.put("{verCode}", verifyCode);
        constant.put("{address}", activity.getPlace());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("orderId", memberActId);
        constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
        constant.put("createBy", memberAct.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activityId));
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(COURSE_APPLY_CODE, memberAct.getMobile(), memberAct.getMemberId(), constant);
    }

	public void getActivityPrtner(Activity activity, HashMap<String, Object> constant){
        MemberGroup member = memberGroupService.getPartner(activity.getMember());
        constant.put("{partner}", Strings.nullToEmpty(member.getRealname()));
    }


    /**
     * 所有众筹手动推送
     */
	public void sendAllCrowdfund(){
        List<ProjectWithAuthor> allList = projectService.allList();
        this.handSendCrowdfund(allList, ALL_CROWDFUND);
    }

    /**
     * 众筹成功推送
     */
    public void sendSuccessCrowdfund(){
        List<ProjectWithAuthor> allList = projectService.successList();
        this.handSendCrowdfund(allList, SUCCESS_CROWDFUND);
    }

    /**
     * 众筹中推送
     */
    public void sendUnderwayCordfund(){
        List<ProjectWithAuthor> allList = projectService.underwayList();
        this.handSendCrowdfund(allList, UNDERWAY_CORDFUND);
    }

    /**
     * 推送所有众筹
     * @param targetId 目标编号
     * @param targetTemplate 模板类容
     */
    public boolean sendAllCrowdfund(String targetId, String authorId, TargetTemplate targetTemplate, String way) {
        List<ProjectWithAuthor> allList = projectService.allList(targetId);
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, allList.size());
        if (isCanFlag) {
            this.handSendCrowdfund(allList, ALL_CROWDFUND, targetTemplate, targetId, authorId, way);
        }
        return isCanFlag;
    }

    /**
     * 发送成功众筹
     * @param targetId 目标编号
     * @param targetTemplate 模板
     */
    public boolean sendSuccessCrowdfund(String targetId, String authorId, TargetTemplate targetTemplate, String way) {
        List<ProjectWithAuthor> allList = projectService.successList(targetId);
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, allList.size());
        if (isCanFlag) {
            this.handSendCrowdfund(allList, SUCCESS_CROWDFUND, targetTemplate, targetId, authorId, way);
        }
        return isCanFlag;
    }

    /**
     * 发送
     * @param targetId 目标编号
     * @param targetTemplate 模板
     */
    public boolean sendUnderwayCordfund(String targetId, String authorId, TargetTemplate targetTemplate, String way) {
        List<ProjectWithAuthor> allList = projectService.underwayList(targetId);
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, allList.size());
        if (isCanFlag) {
            this.handSendCrowdfund(allList, UNDERWAY_CORDFUND, targetTemplate, targetId, authorId, way);
        }
        return isCanFlag;
    }

    /**
     * 手动推送众筹
     * @param list 众筹列表
     * @param eventCode 消息事件
     * @param targetTemplate 模板编号
     * @param targetId 目标编号
     * @param authorId 会员编号
     */
    public void handSendCrowdfund(List<ProjectWithAuthor> list, String eventCode, TargetTemplate targetTemplate ,String targetId, String authorId, String way){
        for (ProjectWithAuthor projectWithAuthor : list){
            HashMap<String, Object> constant = Maps.newHashMap();
            String user = projectWithAuthor.getAuthorName() == null ? "" : projectWithAuthor.getAuthorName();
            constant.put("{user}", user);
            constant.put("{project}", projectWithAuthor.getTitle());
            constant.put("{favorerNum}", projectWithAuthor.getFavorerNum());
            constant.put("{actualAmount}", projectWithAuthor.getActualAmount());
            constant.put("logo", Message.MSG_LOGO_ACT);
            constant.put("relId", projectWithAuthor.getId());
            constant.put("tag", MessageTag.UNDEFIND.getCode());
            constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
            constant.put("createBy", projectWithAuthor.getAuthorId());
            constant.put("targetId", Strings.nullToEmpty(targetId));
            constant.put("senderId", Strings.nullToEmpty(authorId));
            constant.put("way", way);
            constant.put(InstanceConstant.IS_ACTIVE_PUSH, YesNoStatus.YES.getCode()); // 是否主动推送
            getProjectPartner(projectWithAuthor.getId(), constant);
            if (null == targetTemplate || null == targetTemplate.getTemplate()){
                notifyService.push(eventCode, projectWithAuthor.getAuthorMobile(), projectWithAuthor.getAuthorId(), constant);
            }
            else {
                constant.put("templateId", targetTemplate.getId());
                notifyService.push(eventCode, projectWithAuthor.getAuthorMobile(), projectWithAuthor.getAuthorId(), constant, targetTemplate.getTemplate());
            }
        }
    }


    /**
     * 手动推送众筹
     * @param list 众筹列表
     * @param eventCode 时间编号
     */
    private void handSendCrowdfund(List<ProjectWithAuthor> list,String eventCode){
        this.handSendCrowdfund(list, eventCode, null, null, null, null);
    }
    
    /**
     * 表单填写成功推送
     */
    public void sendSuccessForm(String telephone, GatherProject gatherProject) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("createBy", gatherProject.getCreateBy());
        constant.put("logo", Message.MSG_LOGO_TZ);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("{formName}", gatherProject.getTitle());
        constant.put("{currentTime}", DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm"));
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
    	notifyService.push(SUCCESS_FORM, telephone, gatherProject.getCreateBy(), constant);
    }

    /**
     * 会员升级成功推送
     *
     * @param memberId 用户id
     * @param title    套餐标题
     * @param phone    手机号
     */
    public void sendBuyLevel(String memberId, String title, String phone) {
        HashMap<String, Object> constant = Maps.newHashMap();
        Member member = memberService.get(memberId);
        constant.put("{user}", member.getRealname());
        constant.put("{title}", title);
        constant.put("createBy", memberId);
        constant.put("logo", Message.MSG_LOGO_TZ);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        notifyService.push(SUCCESS_BUY_LEVEL, phone, memberId, constant);
    }

    /**
     * 预警
     * @param telephone 手机号
     * @param tableName 表名
     */
    public void sendWarn(String telephone ,String tableName) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{tableName}", tableName);
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(DATA_WARN, telephone, null, constant);
    }

    /**
     * 票券领取成功推送
     *
     * @param telephone  手机号
     * @param couponId   票券id
     * @param verifyCode 核销码
     */
    public void sendCouponOrder(String telephone, String couponId, String verifyCode, Gift gift) {
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        Coupon coupon = couponService.get(couponId);
        HashMap<String, Object> constant = Maps.newHashMap();
//        constant.put("{couponName}", coupon.getName());
//        constant.put("{verCode}", verifyCode);
//        constant.put("{explain}", coupon.getExplain());
        constant.put("{giftName}", gift.getName());
        constant.put("{price}", decimalFormat.format(gift.getPrice()));
        constant.put("{startTime}", DateUtils.formatDate(coupon.getStartTime(), "yyyy-MM-dd"));
        constant.put("{endTime}", DateUtils.formatDate(coupon.getEndTime(), "yyyy-MM-dd"));
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(SUCCESS_COUPON_ORDER, telephone, null, constant);
    }

    /**
     * 实物礼包领取推送
     *
     * @param telephone 手机号
     * @param gift      礼包
     */
    @Override
    public void sendGiftOrder(String telephone, Gift gift) {
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{giftName}", gift.getName());
        constant.put("{price}", decimalFormat.format(gift.getPrice()));
        constant.put("senderId", Strings.nullToEmpty(ADMIN_ID));
        notifyService.push(SUCCESS_GIFT_ORDER, telephone, null, constant);
    }

    /**
     * 提醒打卡
     * @param projectId 项目id
     * @param signCount 打卡人数
     * @param authorId 未打卡用户IDS
     */
    @Override
    public void notifySign(String projectId, Integer signCount, String authorId) throws Exception {
        SignProject signProject = signProjectService.get(projectId);

        JSONObject keynote1 = new JSONObject();
        keynote1.put("value", "有人喊你回来打卡啦~");

        JSONObject keynote2 = new JSONObject();
        keynote2.put("value", signProject.getTitle());
        keynote2.put("color", "#e8473f");

        JSONObject keynote3 = new JSONObject();
        keynote3.put("value", signCount + " 人在打卡，就差你了~\n 立即打卡▶▶");
        keynote3.put("color", "#e8473f");

        JSONObject jsonData = new JSONObject();
        jsonData.put("keyword1", keynote1);
        jsonData.put("keyword2", keynote2);
        jsonData.put("keyword3", keynote3);

        HashMap<String, Object> constant = Maps.newHashMap();
        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", projectId);

        String type = Constant.WECHAT_ACCOUNT_TYPE_SYSTEM_XCX_SIGN;
        constant.put("memberId", authorId);
        wechatKeynotService.getWechatXcxConstant(type, NOTIFY_SIGN, authorId, constant, jsonData, urlConstant);
        notifyService.push(NOTIFY_SIGN, "", authorId, constant);
    }

    /**
     * 打卡成功通知
     * @param projectId 项目id
     * @param authorId  用户id
     * @param applyId   报名id
     */
    @Override
    public void signSuccess(String projectId, String authorId, String applyId) {
        SignProject signProject = signProjectService.get(projectId);
        SignRecord today = signRecordService.getToday(applyId);

        JSONObject keynote1 = new JSONObject();
        keynote1.put("value", "打卡成功~");

        JSONObject keynote2 = new JSONObject();
        keynote2.put("value", signProject.getTitle());

        JSONObject keynote3 = new JSONObject();
        keynote3.put("value", DateUtils.formatDate(today.getCreateDate(), DateUtils.HOUR_MINUTE));

        JSONObject keynote4 = new JSONObject();
        keynote4.put("value", "记得坚持打卡哟~");

        JSONObject jsonData = new JSONObject();
        jsonData.put("keyword1", keynote1);
        jsonData.put("keyword2", keynote2);
        jsonData.put("keyword3", keynote3);
        jsonData.put("keyword4", keynote4);

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("memberId", authorId);
        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", projectId);

        String type = Constant.WECHAT_ACCOUNT_TYPE_SYSTEM_XCX_SIGN;

        try {
            wechatKeynotService.getWechatXcxConstant(type, SUCCESS_SIGN, authorId, constant, jsonData, urlConstant);
            notifyService.push(SUCCESS_SIGN, "", authorId, constant);
        } catch (Exception e) {
            logger.error("发送小程序模板消息异常", e);
        }
    }

    /**
     * 打卡排名通知
     *
     * @param projectId 项目id
     * @param authorId  用户id
     * @param rankNo    名次
     * @param stepNum   步数
     */
    @Override
    public void notifySignRank(String projectId, String authorId, Integer rankNo, Long stepNum) {
        SignProject signProject = signProjectService.get(projectId);

        JSONObject keynote1 = new JSONObject();
        keynote1.put("value", signProject.getTitle());

        JSONObject keynote2 = new JSONObject();
        keynote2.put("value", stepNum);

        JSONObject keynote3 = new JSONObject();
        keynote3.put("value", "昨日排名第" + rankNo + "名");

        JSONObject jsonData = new JSONObject();
        jsonData.put("keyword1", keynote1);
        jsonData.put("keyword2", keynote2);
        jsonData.put("keyword3", keynote3);

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("memberId", authorId);
        HashMap<String, Object> urlConstant = Maps.newHashMap();
        urlConstant.put("{id}", projectId);

        try {
            String type = Constant.WECHAT_ACCOUNT_TYPE_SYSTEM_XCX_SIGN;
            wechatKeynotService.getWechatXcxConstant(type, NOTIFY_SIGN_RANK, authorId, constant, jsonData, urlConstant);
            notifyService.push(NOTIFY_SIGN_RANK, "", authorId, constant);
        } catch (Exception e) {
            logger.error("发送小程序模板消息异常", e);
        }
    }
    /**
     * 发送微信公众号海报
     *
     * @param authorId            接受者
     * @param targetMemberId           目标商户
     * @param type            文件类型
     * @param file 文件
     */
    @Override
    public void postersSend(String authorId,String targetMemberId,String type,File file) throws Exception {
        //获取
        String accessToken = null;
        String appid = null;
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.findByMemberId(targetMemberId);
        if (null != wechatOpenMember) {
            accessToken = wechatOpenMember.getAuthorizerAccessToken();
            appid = wechatOpenMember.getAuthorizerAppid();
        }
        List<ThirdPartyUser> tpus = new ArrayList<>();
        //判断该商户是否有公众号
        if(StringUtils.isEmpty(accessToken)  || StringUtils.isEmpty(appid)){
            wechatOpenMember = wechatOpenMemberService.getSystem();
            accessToken = wechatOpenMember.getAuthorizerAccessToken();
            appid = wechatOpenMember.getAuthorizerAppid();
        }
        tpus = thirdPartyUserService.get(3, authorId, appid);
        logger.info("众筹成功海报推送_tpus_", "accessToken="+accessToken+"_____appid="+appid+"___tpus="+tpus);
        for (ThirdPartyUser thirdPartyUser : tpus) {
            wechatNotifyService.customerServiceSend(thirdPartyUser.getThirdPartyId(),type,accessToken,file);
        }
    }

    @Override
    public boolean multipleProjectSend(String targetId, String authorId, TargetTemplate targetTemplate, String way, String projectIds) {
        String[] projectIdArray = projectIds.split(",");
        HashMap<String, Object> queryParams = Maps.newHashMap();
        queryParams.put("projectIds", projectIdArray);
        List<ProjectWithAuthor> projectWithAuthors = projectService.batchList(queryParams, null);

        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, projectWithAuthors.size());
        if (isCanFlag) {
            this.handSendCrowdfund(projectWithAuthors, MULTIPLE_PUSH, targetTemplate, targetTemplate.getTargetId(), authorId, way);
        }
        return isCanFlag;
    }

    /**
     * 发起众筹消息推送__多场
     * @param project 众筹
     * @param member 会员编号
     */
    @Override
    public void gradationSendLaunchProject(Project project, Member member) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{gradationName}", project.getGradationName());
        constant.put("{targetAmount}", project.getTargetAmount());
        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());
        getProjectSender(project.getId(), constant);
        getProjectPartner(project.getId(), constant);


        logger.info("发起众筹消息推送__多场"+MULTIPLE_LAUNCH_PROJECT+"____"+member.getMobile()+"____"+member.getId()+"____"+constant);
        notifyService.push(MULTIPLE_LAUNCH_PROJECT, member.getMobile(), member.getId(), constant);
    }


    /**
     * 众筹成功发送__多场
     * @param orderForm 订单
     * @param project project
     * @param member 会员信息
     */
    @Override
    public void gradationSendCorowdfund(OrderForm orderForm, Project project, Member member) {

        Activity activity = activityService.get(orderForm.getGoodsId());
        MemberGroup orgMember = memberGroupService.get(activity.getMember());

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", project.getTitle());
        constant.put("{gradationName}", project.getGradationName());
        constant.put("{address}", activity.getPlace());
        constant.put("{user}", member.getRealname());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("orderId", orderForm.getId());
        constant.put("orderStatus", orderForm.getStatus());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", orderForm.getMemberId());
        constant.put("targetId", Strings.nullToEmpty(activity.getId()));
        constant.put("senderId", Strings.nullToEmpty(activity.getMember()));

        String partnerName = getPartnerName(orgMember);
        constant.put("{partner}", Strings.nullToEmpty(partnerName));

        constant.put(InstanceConstant.WAY, InstanceWay.ORGANIZATION.getName());

        //微信推送
        JSONObject jsonData = new JSONObject();
        JSONObject keyword1 = wechatKeynotService.get(orderForm.getTitle());//标题
        JSONObject keyword2 = wechatKeynotService.get(project.getTargetAmount().toString());//标题
        JSONObject keyword3 = wechatKeynotService.get(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm"));//时间

        JSONObject first = wechatKeynotService.get("恭喜贺喜，您在[" + orgMember.getRealname() + "]"+project.getGradationName()+"的众筹活动已经成功");
        JSONObject remark = wechatKeynotService.get("[" + orgMember.getRealname() + "]温馨提醒");

        jsonData.put("first", first);
        jsonData.put("keyword1",keyword1);
        jsonData.put("keyword2", keyword2);
        jsonData.put("keyword3", keyword3);
        jsonData.put("remark", remark);

        HashMap<String, Object> urlConstant = Maps.newHashMap();
        TargetProject targetProject = targetProjectService.findByOrderId(orderForm.getId());
        urlConstant.put("{id}", targetProject.getProjectId());
        constant = wechatKeynotService.getWechatConstant(activity.getMember(), CROWFUND, member.getId(), constant, jsonData, urlConstant);

        CrowdfundEvent crowdfundEvent = crowdfundEventService.findByProjectId(targetProject.getProjectId());
        if (null != crowdfundEvent && YesNoStatus.YES.getCode().equals(crowdfundEvent.getIsMultiple())) {
            constant.put("templateUrl", domain + "/micWeb/html/project/project_multi_detail.html?id=" + targetProject.getProjectId());
        }

        logger.info("众筹成功通知---通知众筹者-------------------------"+MULTIPLE_CROWFUND_SUCCESS+"------"+orderForm.getPhone()+"------"+ orderForm.getMemberId()+"---"+constant);

        notifyService.push(MULTIPLE_CROWFUND_SUCCESS, orderForm.getPhone(), orderForm.getMemberId(), constant);
    }
}
